package pl.umk.mat.fred.unicore.urc.storagemanagement.views;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.preference.IPreferenceStore;

import pl.umk.mat.fred.unicore.urc.storagemanagement.StorageManagementActivator;
import pl.umk.mat.fred.unicore.urc.storagemanagement.preferences.PreferenceConstants;
import pl.umk.mat.fred.unicore.urc.storagemanagement.unicore.Storage;

public class TimeMeterModelProvider {
	
	public static final String BYTES_PER_SECOND = "B/s";
	public static final String KILOBYTES_PER_SECOND = "KB/s";
	public static final String MEGABYTES_PER_SECOND = "MB/s";
	
	private Integer max;
	private String transferUnit; 
	
	public List<Map<String, Object>> getInput() {
		max = -1;
		Map<String, List<Integer>> map = StorageManagementActivator.getDefault().getSpeedMap();
		List<Storage> storages = StorageManagementActivator.getDefault().getStorageList();
		List<Map<String, Object>> responce = new ArrayList<Map<String,Object>>();
		
		List<Object> ll = new ArrayList<Object>();
		Map<String, Object> mm = new HashMap<String, Object>();
		
		ll.add("localhost");
		mm.put("name", "localhost");
		ll.add("");
		mm.put("localhost", null);
		responce.add(mm);
		Integer tmp;
		for (Storage s : storages) {
			ll.add( converTimes2Speed( map.get("localhost " + s.getUrl()) ) );
			tmp = converTimes2Speed( map.get("localhost " + s.getUrl()) );
			mm.put(s.getUrl(), tmp);
			checkMax(tmp);
			
			Map<String, Object> m = new HashMap<String, Object>();
			responce.add(m);
			List<Object> l = new ArrayList<Object>();
			l.add(s.getName());
			m.put("name", s.getName());
			tmp = converTimes2Speed( map.get(s.getUrl() + " localhost"));
			l.add(tmp);
			checkMax(tmp);
			tmp = converTimes2Speed( map.get(s.getUrl() + " localhost"));
			m.put("localhost", tmp);
			checkMax(tmp);
			
			for (Storage s2 : new ArrayList<Storage>(storages)) {
				if (!s.getUrl().equals(s2.getUrl())) {
					l.add( converTimes2Speed( map.get(s.getUrl() + " " + s2.getUrl()) ) );
					tmp = converTimes2Speed( map.get(s.getUrl() + " " + s2.getUrl()));
					m.put(s2.getUrl(), tmp);
					checkMax(tmp);
				} else {
					l.add("");
					m.put(s2.getUrl(), null);
				}
			}
		}
		transferUnit = getTransferUnit(max);
		return responce;
	}
	
	private static Integer converTimes2Speed(List<Integer> times) {
		if (times == null) {
			return null;
		}
		double responce = 0;
		for (Integer i : times) {
			responce += i;
		}
		if (responce == 0) {
			return -1;
		}
		IPreferenceStore store = StorageManagementActivator.getDefault().getPreferenceStore();
		responce = responce / times.size();
		responce = store.getInt(PreferenceConstants.P_TEST_FILE_SIZE) * 1024 * 1000 / responce; // File size in KB, time in ms
		
		return (int)responce;
	}
	
	public static String getTransferUnit(int speed) {
		if (speed > 1024) {
			speed = speed / 1024;
			if (speed > 1024) {
				speed = speed / 1024;
				return MEGABYTES_PER_SECOND;
			}
			return KILOBYTES_PER_SECOND;
		}
		return BYTES_PER_SECOND;
	}
	
	public String getTransferUnit() {
		return transferUnit;
	}
	
	public int getMax() {
		return max;
	}
	
	private void checkMax(Integer val) {
		if (val == null) {
			return;
		}
		if (val > max) {
			max = val;
		}
	}
}
